﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Business
{
    public class ApkFileBiz
    {
        public static string GetApk(string key, string imgstyle = "")
        {
            if (!string.IsNullOrWhiteSpace(key))
            {
                if (key.ToLower().StartsWith("http://") || key.ToLower().StartsWith("https://"))
                    return key;
                else
                    return AliyunOssBiz.GetImg(key, imgstyle);
            }
            else
                return "";
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <returns></returns>
        public static Tuple<bool, string, string> UpFile(HttpPostedFile file)
        {
            if (file == null || string.IsNullOrWhiteSpace(file.FileName))
                return new Tuple<bool, string, string>(false, "文件为空", "");


            var contentType = file.ContentType;
            var len = file.ContentLength;
            var fileType = new string[] { "application/vnd.android.package-archive", };
            if (!fileType.Any(b => b.Contains(contentType)))
            {
                return new Tuple<bool, string, string>(false, $"_{file.FileName}只能上传apk格式的文件。", "");
            }

            if (len < 1024 * 1024 * 5)
            {
                return new Tuple<bool, string, string>(false, "apk太小了吧", "");
            }

            string fileKey = "apk/" + Path.GetFileName(file.FileName);

            string bucketNameAppk = ConfigHelper.AppSetting<string>("aliyunoss_apk_bucketname", "daneel-apk");
            var result = AliyunOssBiz.PutObjectWithStream(bucketNameAppk, fileKey, file.InputStream);
            if (result != "")
                return new Tuple<bool, string, string>(true, "", AliyunOssBiz.GetFile(bucketNameAppk, fileKey));
            else
                return new Tuple<bool, string, string>(false, "上传失败", "");
        }
    }
}
